package com.endpoint.author.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.endpoint.author.entity.Author;
import com.endpoint.author.feign.BookFeignService;
import com.endpoint.author.service.AuthorService;
import com.endpoint.author.to.AuthorTo;
import com.endpoint.author.to.BookIndexTo;
import com.endpoint.author.to.BookTo;
import com.endpoint.author.vo.AuthorVo;
import com.endpoint.author.vo.BookAddVo;
import com.endpoint.author.vo.ChapterAddVo;
import com.endpoint.common.constant.ResponseStatus;
import com.endpoint.common.exception.BusinessException;
import com.endpoint.common.utils.JwtTokenUtil;
import com.endpoint.common.utils.ResultBean;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.awt.print.Book;
import java.util.List;

/**
 * <p>
 * 作者表 前端控制器
 * </p>
 *
 * @author Jack_Li
 * @since 2024-07-03
 */
@RestController
@RequestMapping("/author")
public class AuthorController {

    @Autowired
    private AuthorService authorService;

    @GetMapping()
    public List getAll(){
        List<Author> authors = authorService.list();
        if(true) {
            throw new BusinessException(ResponseStatus.ERROR);
        }
        return authors;
    }

    @GetMapping("/status")
    public ResultBean<Integer> getAuthorStatus(@RequestHeader("Authorization") String token ){
        Long userId = JwtTokenUtil.getUserIdFromToken(token);
        return authorService.getAuthorStatus(userId);
    }

    @PostMapping("/register")
    public ResultBean<Void> register(@Valid @RequestBody AuthorVo authorVo, @RequestHeader("Authorization") String token) {
        Long userId = JwtTokenUtil.getUserIdFromToken(token);
        authorVo.setUserId(userId);
        return authorService.register(authorVo);
    }

    //查询作家发布书籍集合
    @GetMapping("/books")
    public ResultBean<Page<BookTo>> listBooks(@RequestParam(defaultValue ="1", name = "pageNum") int pageNum,
                                              @RequestParam(defaultValue = "10",name = "pageSize") int pageSize,
                                              @RequestHeader("Authorization") String token){
        Long userId = JwtTokenUtil.getUserIdFromToken(token);
        ResultBean<Page<BookTo>>  result = authorService.listBooks(pageNum,pageSize,userId);
        return result;
    }

    //小说发布接口
    @ApiOperation(value = "小说发布接口")
    @PostMapping("/book")
    public ResultBean<Void> publishBook(@Valid @RequestBody BookAddVo vo, HttpServletRequest request) {
        String token = request.getHeader("Authorization");

        return authorService.saveBook(vo, token);
    }

    //发布章节
    @ApiOperation(value = "发布章节")
    @PostMapping("/chapter/{bookId}")
    public ResultBean<Void> saveBookChapter(@PathVariable("bookId") Long bookId, @Valid @RequestBody ChapterAddVo vo, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        Long userId = JwtTokenUtil.getUserIdFromToken(token);
        return authorService.saveBookChapter(bookId, vo, userId);
    }

    @GetMapping("/chapters/{bookId}")
    public ResultBean<Page<BookIndexTo>> listChapters(@PathVariable("bookId") Long bookId,
                                                      @RequestParam(defaultValue ="1", name = "pageNum") int pageNum,
                                                      @RequestParam(defaultValue = "10",name = "pageSize") int pageSize
    ) {
        return authorService.listChapters(pageNum,pageSize, bookId);
    }

    @GetMapping("/getAuthor/{memberId}")
    public ResultBean<AuthorTo> getAuthorByMemberId(@PathVariable("memberId") Long memberId){
        Author author = authorService.getOne(new QueryWrapper<Author>().eq("member_id", memberId));
        AuthorTo authorTo = new AuthorTo();
        if(author != null) {
            BeanUtils.copyProperties(author, authorTo);
        }
        return ResultBean.ok(authorTo);
    }

}

